package com.personas.analysic.analyserv.util;

import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;


public class HbaseUtil {
    private static Admin admin;
    private static Connection conn;



    static{
        ParameterTool tool = null;
        try {
            tool = ParameterTool.fromPropertiesFile("E:\\code\\PersonasAdmin\\analyserv\\src\\main\\resources\\flink.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建hbase配置对象
        Configuration conf = HBaseConfiguration.create();
        //使用eclipse时必须添加这个，否则无法定位
        conf.set("hbase.zookeeper.quorum",tool.getRequired("hbase.zookeeper.quorum"));
        conf.set("hbase.zookeeper.property.clientPort",tool.getRequired("hbase.zookeeper.property.clientPort"));
        conf.set("zookeeper.znode.parent",tool.getRequired("zookeeper.znode.parent"));
        try {
            conn = ConnectionFactory.createConnection(conf);
            // 得到管理程序
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void putdata(String tablename, String rowkey, String famliyname,String colum,String data) throws Exception {
        Table table = conn.getTable(TableName.valueOf(tablename));
        Put put = new Put(rowkey.getBytes());
        put.addColumn(famliyname.getBytes(),colum.getBytes(),data.getBytes());
        table.put(put);
    }

}
